package yannan.voiplock4ble.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;

import yannan.voiplock4ble.BuildConfig;
import yannan.voiplock4ble.LoginActivity;
import yannan.voiplock4ble.task.OutLogTimer;
import yannan.voiplock4ble.utils.ActivityCollector;
import yannan.voiplock4ble.utils.PopShowUtils;

/**
 * 说明：已经登录的页面基类
 * 作者：mainTel
 * 时间：2016/12/15 18:21
 * 备注：
 */
public abstract class LoggedBaseActivity extends BaseActivity {

    OutLogTimer outLogTimer;
    Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //透明导航栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        outLogTimer = OutLogTimer.create(System.currentTimeMillis());
        timer = new Timer();
        timer.schedule(outLogTimer, 0, 5000);
        ActivityCollector.addActivity(this);
        if (BuildConfig.DEBUG) Log.e("LoggedBaseActivity", "onCreate");
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        outLogTimer.setTime(System.currentTimeMillis());
        return super.dispatchTouchEvent(event);
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        outLogTimer.setTime(System.currentTimeMillis());
        if (BuildConfig.DEBUG) Log.e("LoggedBaseActivity", "dispatchKeyEvent");
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        outLogTimer.setTime(System.currentTimeMillis());
        if (BuildConfig.DEBUG) Log.e("LoggedBaseActivity", "onKeyDown");
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        outLogTimer.cancel();
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String msg) {
        if (msg.equals(MyEnum.OutLog.OUT_LOG.toString())) {
            outLogTimer.cancel();
            PopShowUtils.showCommonDialog(mContext, "登录已过期，请重新登录", null, "确定", false,
                    new PopShowUtils.IDialogCommonListener() {
                        @Override
                        public void onClick(int direction) {
                            Intent intent = new Intent(mContext, LoginActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            ActivityCollector.finishAll();
                        }
                    });
        }
    }

}
